package com.fuint.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuint.framework.annoation.OperationServiceLog;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationRequest;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.repository.model.MtDeviceItem;
import com.fuint.common.service.DeviceItemService;
import com.fuint.common.enums.StatusEnum;
import com.fuint.repository.mapper.MtDeviceItemMapper;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

/**
 * 体检项目明细表服务接口
 *
 * Created by whileu
 * CopyRight https://www.fuint.cn
 */
@Service
@AllArgsConstructor
public class DeviceItemServiceImpl extends ServiceImpl<MtDeviceItemMapper, MtDeviceItem> implements DeviceItemService {

    private static final Logger logger = LoggerFactory.getLogger(DeviceItemServiceImpl.class);

    private MtDeviceItemMapper mtDeviceItemMapper;

    /**
     * 分页查询数据列表
     *
     * @param paginationRequest
     * @return
     */
    @Override
    public PaginationResponse<MtDeviceItem> queryDeviceItemListByPagination(PaginationRequest paginationRequest) {
        Page<MtDeviceItem> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        LambdaQueryWrapper<MtDeviceItem> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.ne(MtDeviceItem::getStatus, StatusEnum.DISABLE.getKey());

        lambdaQueryWrapper.orderByAsc(MtDeviceItem::getId);
        List<MtDeviceItem> dataList = mtDeviceItemMapper.selectList(lambdaQueryWrapper);

        PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
        PaginationResponse<MtDeviceItem> paginationResponse = new PaginationResponse(pageImpl, MtDeviceItem.class);
        paginationResponse.setTotalPages(pageHelper.getPages());
        paginationResponse.setTotalElements(pageHelper.getTotal());
        paginationResponse.setContent(dataList);

        return paginationResponse;
    }

    /**
     * 添加体检项目明细表
     *
     * @param mtDeviceItem 体检项目明细表信息
     * @return
     */
    @Override
    @OperationServiceLog(description = "新增体检项目明细表")
    public MtDeviceItem addDeviceItem(MtDeviceItem mtDeviceItem) throws BusinessCheckException {
        mtDeviceItem.setStatus(StatusEnum.ENABLED.getKey());
        mtDeviceItem.setUpdateTime(new Date());
        mtDeviceItem.setCreateTime(new Date());
        Integer id = mtDeviceItemMapper.insert(mtDeviceItem);
        if (id > 0) {
            return mtDeviceItem;
        } else {
            throw new BusinessCheckException("新增体检项目明细表数据失败");
        }
    }

    /**
     * 根据ID获体检项目明细表取息
     *
     * @param id 体检项目明细表ID
     * @return
     */
    @Override
    public MtDeviceItem queryDeviceItemById(Integer id) {
        return mtDeviceItemMapper.selectById(id);
    }

    /**
     * 根据ID删除体检项目明细表
     *
     * @param id 体检项目明细表ID
     * @param operator 操作人
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "删除体检项目明细表")
    public void deleteDeviceItem(Integer id, String operator) {
        MtDeviceItem mtDeviceItem = queryDeviceItemById(id);
        if (null == mtDeviceItem) {
            return;
        }
        mtDeviceItem.setStatus(StatusEnum.DISABLE.getKey());
        mtDeviceItem.setUpdateTime(new Date());
        mtDeviceItemMapper.updateById(mtDeviceItem);
    }

    /**
     * 修改体检项目明细表数据
     *
     * @param mtDeviceItem
     * @throws BusinessCheckException
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "更新体检项目明细表")
    public MtDeviceItem updateDeviceItem(MtDeviceItem mtDeviceItem) throws BusinessCheckException {
        mtDeviceItem = queryDeviceItemById(mtDeviceItem.getId());
        if (mtDeviceItem == null) {
            throw new BusinessCheckException("该体检项目明细表状态异常");
        }
        mtDeviceItem.setUpdateTime(new Date());
        mtDeviceItemMapper.updateById(mtDeviceItem);
        return mtDeviceItem;
    }

   /**
    * 根据条件搜索体检项目明细表
    *
    * @param  params 查询参数
    * @throws BusinessCheckException
    * @return
    * */
    @Override
    public List<MtDeviceItem> queryDeviceItemListByParams(Map<String, Object> params) {
        String status =  params.get("status") == null ? StatusEnum.ENABLED.getKey(): params.get("status").toString();
        String storeId =  params.get("storeId") == null ? "" : params.get("storeId").toString();
        String merchantId =  params.get("merchantId") == null ? "" : params.get("merchantId").toString();

        LambdaQueryWrapper<MtDeviceItem> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(status)) {
            lambdaQueryWrapper.eq(MtDeviceItem::getStatus, status);
        }
//        if (StringUtils.isNotBlank(merchantId)) {
//            lambdaQueryWrapper.eq(MtDeviceItem::getMerchantId, merchantId);
//        }
//        if (StringUtils.isNotBlank(storeId)) {
//            lambdaQueryWrapper.and(wq -> wq
//                    .eq(MtDeviceItem::getStoreId, 0)
//                    .or()
//                    .eq(MtDeviceItem::getStoreId, storeId));
//        }

        lambdaQueryWrapper.orderByAsc(MtDeviceItem::getId);
        List<MtDeviceItem> dataList = mtDeviceItemMapper.selectList(lambdaQueryWrapper);
        return dataList;
    }
}
